<template>
  <div>
    <el-dialog
      class="car-owner-add"
      :title="isEdit ? '信息修改' : '新增摄像头设备'"
      :visible.sync="dialogVisible"
      width="90%"
      :before-close="handleClose"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
      <el-row>
        <el-col :span="10">
            <div style="width:100%;height:450px;position:relative;margin-bottom:10px">
                <org-map @map-serach="doPosition" style="position: absolute;top: 0;right: 10px;z-index: 1;"></org-map>
                <div id="container" style="width:100%;height:100%"></div>
            </div>
        </el-col>
        <el-col :span="14" style="height:100%">
          <el-row>
            <el-col :span="12">
                <el-form-item label="所在小区" prop="orgId">
                  <org-select v-model="ruleForm.orgId" @select="orgSelect"></org-select>
                </el-form-item>
              </el-col>
            <el-col :span="12">
              <el-form-item label="设备名称" prop="cameraEquipmentName">
                <el-input v-model="ruleForm.cameraEquipmentName"></el-input>
              </el-form-item>
            </el-col>
          
          </el-row>
          <el-row>
            <el-col :span="12">
                <el-form-item label="设备编码" prop="cameraEquipmentCode">
                  <el-input v-model="ruleForm.cameraEquipmentCode"></el-input>
                </el-form-item>
              </el-col>
            <el-col :span="12">
              <el-form-item label="设备序列号" prop="deviceId">
                <el-input v-model="ruleForm.deviceId"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="登录名" prop="account">
                <el-input v-model="ruleForm.account"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="登录密码" prop="passWord">
                <el-input type="password" v-model="ruleForm.passWord"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="IP" prop="ip">
                <el-input v-model="ruleForm.ip"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="端口" prop="port">
                <el-input v-model="ruleForm.port"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
         <el-row>
            <el-col :span="12">
              <el-form-item label="经度" prop="longitude">
                <el-input v-model="ruleForm.longitude"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="纬度" prop="latitude">
                <el-input v-model="ruleForm.latitude"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="详细位置" prop="address">
                <el-input v-model="ruleForm.address"></el-input>
              </el-form-item>
            </el-col>
          
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="设备照片" prop="photo">
                <el-upload
                  class="upload-demo"
                  action="/xds/file/photo-upload"
                  :on-success="uploadSuccess"
                  :limit="5"
                >
                  <el-button type="text" size="small">上传文件</el-button>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
        
      </el-form>

      <span slot="footer" class="dialog-footer">
         <el-col :span="12" style="text-align: right; padding-right: 60px">
            <el-button type="text" @click="getCoordinates"
              >经纬度采集</el-button
            >
          </el-col>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button @click="doReset" v-show="isResetShow">重 置</el-button>
        <el-button type="primary" @click="doConfirm">确 定</el-button>
      </span>
    </el-dialog>
    <coordinates ref="coordinates"></coordinates>
  </div>
</template>


<script>
import * as API from "@/api/business/camera-equipment";
import * as PhotoApi from "@/api/business/photo";
import coordinates from "@/components/coordinates/index";
export default {
  components: { coordinates },
  data() {
    return {
      dialogVisible: false,
      isEdit: false,
      isResetShow: false,
      filePath: null,
      ruleForm: {
        cameraEquipmentName: "",
        cameraEquipmentCode: "",
        deviceId:"",
        longitude: "",
        latitude: "",
        orgId: "",
        orgName: "",
        address: "",
        passWord:"",
        account:"admin",
        ip:"",
        port:"37777"
      },
      rules: {
        cameraEquipmentName: [
          { required: true, message: "请输入摄像头名称", trigger: "blur" },
        ],
        cameraEquipmentCode: [
          { required: true, message: "请输入摄像头编码", trigger: "blur" },
        ],
        deviceId: [
          { required: true, message: "请输入序列号", trigger: "blur" },
        ],
        longitude: [{ required: true, message: "请选择经度", trigger: "blur" }],
        latitude: [{ required: true, message: "请输入纬度", trigger: "blur" }],
        account: [{ required: true, message: "请输入设备登陆用户名", trigger: "blur" }],
        passWord: [{ required: true, message: "请输入设备登陆密码", trigger: "blur" }],
        address: [
          {
            required: true,
            message: "请输入摄像头安装位置的详细信息",
            trigger: "blur",
          },
        ],
        orgId: [{ required: true, message: "请选择小区", trigger: "blur" }],
      }
    };
  },
  methods: {
    doPosition(value) {
        let la = value.location.split(',')
        this.map.setCenter(la) //设置地图中心点
        var marker = new AMap.Marker({
            position: la,
            // icon: './img/alarm1.png',
            map: this.map
        })
        this.ruleForm.longitude = la[0]
        this.ruleForm.latitude = la[1]
        this.ruleForm.address = value.address
        this.ruleForm.orgName = value.name
    },
    initMap() {
        this.map = new AMap.Map('container', {
            resizeEnable: true,
            rotateEnable: false,
            pitchEnable: false,
            zoom: 20,
            pitch: 65,
            rotation: 0,
            viewMode: '2D', //开启3D视图,默认为关闭
            zooms: [3, 20]
        })
        this.map.on('click', e => {
            console.log(e)
        })
        this.map.on('complete', function () {
            console.log('地图加载完成')
        })
    },
    show() {
      this.dialogVisible = true;
      this.isResetShow = true
      this.isEdit = false
      this.$nextTick(() => {
          this.doReset()
          this.initMap()
      })
    },
    edit(ruleForm) {
      Object.assign(this.ruleForm, ruleForm);
      this.isEdit = true;
      this.dialogVisible = true;
      this.isResetShow = false
      this.$nextTick(() => {
          this.initMap()
      })
    },
    orgSelect(obj){
        this.ruleForm.orgName = obj.name
    },
    handleClose() {
      this.doReset();
      this.dialogVisible = false;
    },
    doConfirm() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            API.edit(this.ruleForm).then((res) => {
              if (res.data.code === 0) {
                this.$message.success("设备信息修改成功!");
                this.handleClose();
                this.$emit("table-search");
              }
            });
          } else {
            PhotoApi.save({ path: this.filePath }).then((res) => {
              if (res.data.code === 0) {
                this.ruleForm.photoId = res.data.data;
                API.save(this.ruleForm).then((res) => {
                  if (res.data.code === 0) {
                    this.$message.success("新增摄像头成功!");
                    this.handleClose();
                    this.$emit("table-search");
                  }
                });
              }
            });
          }
        }
      });
    },
    doReset() {
      this.$refs["ruleForm"].resetFields();
    },
    uploadSuccess(response) {
      this.filePath = response.data;
      if (response.data.indexOf("/static") > -1) {
        this.filePath = response.data.substr(response.data.indexOf("/static"));
      }
    },
    getCoordinates() {
      this.$refs["coordinates"].show();
    },
  },
};
</script>

<style lang="scss" scoped>
.car-owner-add {
  .el-input {
    width: 220px;
  }
}
</style>